﻿using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.Channel;
using SuperSocket.Server;

namespace Lawn.AppServer
{
    public class MessageAppSesssion : AppSession
    {



        private ILogger<MessageAppSesssion> logger;

        private ISessionManager sessionManager;
         

        public MessageAppSesssion(ILogger<MessageAppSesssion> _logger,
            ISessionManager _sessionManager)
        {
            this.logger = _logger;
            this.sessionManager = _sessionManager;
           


        }


        protected override async ValueTask OnSessionClosedAsync(CloseEventArgs e)
        {
            var ipEndPoint = RemoteEndPoint as IPEndPoint;
            //var _logger = this.Server.ServiceProvider.GetRequiredService<ILogger<MessageAppSesssion>>();
            await sessionManager.Closed(this);
            logger.LogInformation($"{SessionID} {ipEndPoint.Address} {ipEndPoint.Port}  closed  {e.Reason}");
        }

        protected override async ValueTask OnSessionConnectedAsync()
        {
            var ipEndPoint = RemoteEndPoint as IPEndPoint;
            await sessionManager.Connected(this);
           // var _logger = this.Server.ServiceProvider.GetRequiredService<ILogger<MessageAppSesssion>>();
            logger.LogInformation($" {SessionID} {ipEndPoint.Address}  {ipEndPoint.Port}   connected ");
            
            //Logger.LogInformation($"{sessionContainer.GetSessionCount()}");
        }

    }
}